*Generates TABLE 2: Summary of BCBS Patient Data by Outpatient Provider
*Version 15 Stata

set more off

*Bring in data
use adults_sample_series_merged_treatment_period.dta, clear

*Merge in those who appear in both regression samples
merge m:1 memb_bid using final_overlap_sample.dta
keep if _merge==3

*Restrict sample
keep if month_code>=first_month+1

*Mandate that in entropy sample month
*sample "non-missing entropy sample"
keep if i_full==1 & entropy_molecule!=. & i_npi_str!=.

*Parse variables
keep memb_bid_str serv_yr serv_month prof_cost real_total_cost pharm_cost facility_cost monthly_CPI i_ad2 guide_* month_code sum_i_psych first_month i_ER_hosp* merge_n_violate sum_i_psych

*Grab the months that are in entropy regressions
merge 1:1 memb_bid_str serv_yr serv_month using final_for_stats.dta
keep if _merge==3
drop _merge

*******************************************************************************
*******************************************************************************
*PANEL A
*******************************************************************************
*******************************************************************************

*******************************************************************************
*Table 2, Row 1: #Members
*******************************************************************************
unique memb_bid
unique memb_bid if sum_i_psych>0
unique memb_bid if sum_i_psych==0

*******************************************************************************
*Table 2, Row 2: #Member-months
*******************************************************************************
count
count if sum_i_psych>0
count if sum_i_psych==0

*******************************************************************************
*Table 2, Row 3-6: #Months/member, #Months antidepressants/member, #Changes in entropy/member
*******************************************************************************
preserve

* change in entropy
sort memb_bid_str month_code
bysort memb_bid_str: gen entropy_t=entropy_molecule_avg[_n+1]
gen change_entropy=(entropy_t!=entropy_molecule_avg)

*months / patient; months with ad / patient (includes missing drug transitions)
generate count = 1
keep count i_ad2 memb_bid change_entropy
collapse (sum) change_entropy count i_ad2, by(memb_bid)
summarize count i_ad2 change_entropy

restore

preserve
keep if sum_i_psych>0
* change in entropy
sort memb_bid_str month_code
bysort memb_bid_str: gen entropy_t=entropy_molecule_avg[_n+1]
gen change_entropy=(entropy_t!=entropy_molecule_avg)

*months / patient; months with ad / patient (includes missing drug transitions)
generate count = 1
keep count i_ad2 memb_bid change_entropy
collapse (sum) change_entropy count i_ad2, by(memb_bid)
summarize count i_ad2 change_entropy

restore

preserve
keep if sum_i_psych==0

*Flag if there is a change in entropy
sort memb_bid_str month_code
bysort memb_bid_str: gen entropy_t=entropy_molecule_avg[_n+1]
gen change_entropy=(entropy_t!=entropy_molecule_avg)

*Generate months / patient; months with ad / patient (includes missing drug transitions)
generate count = 1
keep count i_ad2 memb_bid change_entropy
collapse (sum) change_entropy count i_ad2, by(memb_bid)
summarize count i_ad2 change_entropy

restore

*Save temporary file
tempfile temp
save `temp', replace

*Bring in data
cd `d1'
use brth_yr entropy_molecule first_month guide_* i_full memb_bid_str merge_n_violate month_code serv_month serv_yr sum_i_psych using adults_sample_series_merged_treatment_period.dta, clear

*Merge in those who appear in both regression samples
merge m:1 memb_bid using final_overlap_sample.dta
keep if _merge==3

sort memb_bid_str month_code
replace first_month=. if first_month==0 

* %lagged scripts that violate each guideline
local list UK US CA cocktail
foreach var of local list{
	bysort memb_bid_str: gen lag_transition_`var'=guide_`var'[_n-1]
	label var lag_transition_`var' "`var'"
} 

*If first in (t), starts from outcome in (t+1) on characteristics in (t) 
keep if month_code>=first_month+1 

keep if merge_n_violate==3 
*// main sample for violation regressions
keep if lag_transition_US!=. 

*******************************************************************************
*Table 2, Row 7: #Member-months with drug transitions
*******************************************************************************
count
count if sum_i_psych>0
count if sum_i_psych==0

*******************************************************************************
*******************************************************************************
*PANEL B
*******************************************************************************
*******************************************************************************

*Calculate averages
summarize lag_transition_UK lag_transition_CA lag_transition_US lag_transition_cocktail
summarize lag_transition_UK lag_transition_CA lag_transition_US lag_transition_cocktail if sum_i_psych>0
summarize lag_transition_UK lag_transition_CA lag_transition_US lag_transition_cocktail if sum_i_psych==0

*******************************************************************************
*******************************************************************************
*PANELS C and D
*******************************************************************************
*******************************************************************************

*Bring in data
use `temp', clear

*generate real cost variables for subcategories
gen real_prof = prof_cost/(1+monthly_CPI)
gen real_pharm = pharm_cost/(1+monthly_CPI)
gen real_facility = facility_cost/(1+monthly_CPI)

foreach var of varlist real_total_cost real_pharm real_prof real_facility {
summarize `var', d
}
summarize i_ER_hosp i_ER_hosp_mh
foreach var of varlist real_total_cost real_pharm real_prof real_facility {
summarize `var' if sum_i_psych>0, d
}
summarize i_ER_hosp i_ER_hosp_mh if sum_i_psych>0
foreach var of varlist real_total_cost real_pharm real_prof real_facility {
summarize `var' if sum_i_psych==0, d
}
summarize i_ER_hosp i_ER_hosp_mh if sum_i_psych==0
